angular.module('Caltech').controller('equipmentController', function ($scope, Restangular, ngTableParams, $stateParams, $location) {
    'use strict';

    $scope.filters = {};

    $scope.paging = {
        index: 1,
        size: 10
    };

    $scope.filter = false;

    $scope.search = function (index) {
        if ($scope.tableParams) {
            $scope.tableParams.reload();
        } else {
            $scope.tableParams = new ngTableParams({
                page: $scope.paging.index,
                count: $scope.paging.size
            }, {
                counts: [15, 20, 25],
                getData: function ($defer, params) {
                    var model = $scope.filters.model;
                    var description = $scope.filters.description;

                    if ($scope.filter) {
                        $scope.filter = false;
                        params.page(1);
                    }

                    Restangular.all('equipment')
                        .getList({
                            page: params.page(),
                            count: params.count(),
                            model: model || undefined,
                            description: description || undefined
                        }).then(function (equipment) {
                            params.total(equipment.total);
                            $defer.resolve(equipment);

                            if (params.page() != $scope.paging.index) {
                                $location.search('pageIndex', params.page());
                                $scope.paging.index = params.page();
                            }
                            if (params.count() != $scope.paging.size) {
                                $location.search('pageIndex', 1);
                                $location.search('pageSize', params.count());
                                $scope.paging.size = params.count();
                            }

                            $location.search('model', model || undefined);
                            $location.search('description', description || undefined);
                        });
                }
            });
        }

    };

    $scope.getCustomerList = function (nameFitler) {
        return Restangular.all('customer/list').getList({
            name: nameFitler
        });
    };

    $scope.updateState = function (item) {
        if (!item) {
            $scope.customer = {};
        } else if (typeof (item) === 'object') {
            $scope.customer = item;

            $location.search('customerId', item.id);
            $location.search('customerName', item.name);

            if ($scope.tableParams) {
                $scope.tableParams.reload();
            } else {
                $scope.search();
            }
        }
    };

    $scope.import = function (file) {
        alert('Being developed');
    };

    $scope.reSearch = function () {
        $location.search('pageIndex', 1);
        $scope.paging.index = 1;
        $scope.filter = true;
        $scope.search();
    };

    if ($stateParams.model || $stateParams.pageIndex || $stateParams.pageSize) {
        $scope.filters.model = $stateParams.model;
        $scope.filters.description = $stateParams.description;
        $scope.paging = {
            index: $stateParams.pageIndex || 1,
            size: $stateParams.pageSize || 10
        };
        $scope.search();
    } else {
        $scope.search();
    }
});